<!--
 * 岗位管理
 * @author: hexiaozhen
 * @since: 2023-09-27
 * index.vue
-->
<template>
  <div class="main-content">
    <!-- 搜索 -->
    <TableForm
      ref="formAndTableRef"
      :formItems="formItems"
      :formData="formData"
      :girdOption="girdOption"
      class="tableForm"
      @formChange="formChange"
      @getTableList="getTableList"
      @selectionChange="selectionChange"
      @handleBtnClick="handleBtnClick"
    >
      <!-- 自定义操作按钮 -->
      <template #tableOptate="{ record }">
        <a-button
          v-for="clickItem in tableOperateList"
          :key="clickItem.text"
          type="text"
          class="operate-item"
          :status="clickItem.eventName == 'tableDel' ? 'danger' : ''"
          @click="handleBtnClick(clickItem.eventName, record)"
        >
          {{ clickItem.text }}
        </a-button>
      </template>
      <!-- 列插槽 -->
      <template #createdTime="{ record }">
        {{ record.createdTime && record.createdTime.slice(0, 10) }}
      </template>
    </TableForm>
    <AddEditPost v-if="visible" v-model:visible="visible" :rowData="rowData" @submit="getTableList()"></AddEditPost>
    <PersonView v-if="viewVisible" v-model:visible="viewVisible" :rowData="rowData"></PersonView>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue'
import TableForm from '@/components/tableForm/index.vue'
import AddEditPost from './components/addEditPost.vue' // 新增编辑
import PersonView from './components/personView.vue' // 新增编辑
import system from '@/api/modules/system'
import { Modal, Message } from '@arco-design/web-vue'
import { useUserStoreHook } from '@/store/modules/user'
const userStore = useUserStoreHook()

const formData = reactive({ postName: '', deptId: '' })
const formAndTableRef = ref()
// 获取字典数据
const options = ref([])
// 表单配置
const formItems = reactive([
  {
    name: 'deptId',
    label: '部门',
    type: 'a-tree-select',
    data: options,
    noClear: true,
    fieldNames: {
      key: 'deptId',
      title: 'deptName',
      children: 'children',
    },
  },
  { name: 'postName', label: '岗位名称', type: 'a-input' },
])
const girdOption = reactive({
  btnList: [
    { name: '新增', eventName: 'add', type: 'icon-xinzeng' },
    { name: '人员视图', eventName: 'personView', type: 'icon-renyuanshitu' },
    { name: '删除', status: 'danger', eventName: 'delMultiple', type: 'icon-shanchu' },
  ],
  columns: [
    { title: '序号', slotName: 'serialNumber', width: 70 },
    { title: '岗位编号', dataIndex: 'postCode', width: 190 },
    { title: '岗位名称', dataIndex: 'postName', width: 100 },
    { title: '部门', dataIndex: 'deptName', width: 224 },
    { title: '岗位描述', dataIndex: 'postDesc', width: 120 },
    { title: '备注', dataIndex: 'remark', width: 120 },
    { title: '创建时间', slotName: 'createdTime', width: 125 },
    { title: '操作', slotName: 'optional', width: 110, fixed: 'right' },
  ],
  loading: false,
  tableId: 'postId', //表格唯一标识符类似于id
  tableSlotList: ['createdTime'], //表格插槽
  rowSelection: true, // 多选
})

// 表格操作插槽
const tableOperateList = reactive([
  { text: '修改', eventName: 'tableEdit' },
  { text: '删除', eventName: 'tableDel' },
])
let visible = ref(false) // 新增编辑弹窗
let rowData = reactive({ type: 'add' }) // 弹窗数据
let viewVisible = ref(false) // 人员视图弹窗
let selectionList = [] // 多选数据
const handleBtnClick = (type, row) => {
  switch (type) {
    case 'add': {
      // 新增
      rowData = { type: 'add', deptId: formData.deptId }
      visible.value = true
      break
    }
    case 'delMultiple': {
      //批量删除
      if (!selectionList.length) {
        return Message.warning('请先选择表格数据！')
      }
      Modal.open({
        title: '警告',
        content: `是否确认批量删除已选数据项？`,
        draggable: true,
        onOk: () => {
          delPost(selectionList.join(','))
        },
      })
      break
    }
    case 'tableEdit': {
      //编辑
      visible.value = true
      rowData = { ...row, type: 'edit' }
      break
    }
    case 'personView': {
      // 人员视图
      viewVisible.value = true
      rowData = { deptId: formData.deptId }
      break
    }
    case 'tableDel': {
      //删除
      Modal.open({
        title: '警告',
        content: `是否确认删除岗位名称为${row.postName}的数据项？`,
        draggable: true,
        onOk: () => {
          delPost(row.postId)
        },
      })

      break
    }
  }
}
const delPost = (id) => {
  system.delPost(id).then((res) => {
    getTableList()
    Message.success(res.msg)
  })
}
const formChange = (prop) => {
  // 部门筛选直接掉列表接口
  if (prop === 'deptId') {
    formAndTableRef.value.setPageCurrent(1) // 设置表格分页器分页为1
    getTableList({ pageSize: 10, current: 1 })
  }
}
// 获取表格数据
let requestParam = {}
const getTableList = (pageConfig) => {
  if (pageConfig) {
    requestParam.size = pageConfig.pageSize
    requestParam.current = pageConfig.current
  }
  if (!formData.deptId && options.value.length) {
    formData.deptId = options.value[0].deptId
  }
  let param = {
    ...formData,
  }
  girdOption.loading = true
  system
    .getPost(param, requestParam)
    .then((res) => {
      formAndTableRef.value.setTableData(res.data.records, Number(res.data.total))
      girdOption.loading = false
    })
    .catch(() => {
      girdOption.loading = false
    })
}
// 多选
const selectionChange = (list) => {
  selectionList = list
}
// 数据字典 -- 部门下拉
const getOption = () => {
  system.getDeptList({ deptId: userStore.userInfo.deptId }).then((res) => {
    if (res.data.length) {
      options.value = res.data
      formData.deptId = res.data[0].deptId
      getTableList({ pageSize: 10, current: 1 })
    }
  })
}
onMounted(() => {
  getOption()
})
</script>
